package org.xx.armory.obs.autoconfigure;

import com.obs.services.IObsClient;
import com.obs.services.ObsClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.xx.armory.obs.controllers.HuaweiObsController;

/**
 * 自动配置{@literal armory.obs}。
 * <p>相关配置项：</p>
 * <dl>
 *     <dt>{@literal armory.hw-obs.endpoint}</dt>
 *     <dd>华为OBS的访问URL，和区域有关（比如华北一区）</dd>
 *     <dt>{@literal armory.hw-iam.access-key}</dt>
 *     <dd>华为IAM登录帐号的访问Key。</dd>
 *     <dt>{@literal armory.hw-iam.security-key}</dt>
 *     <dd>华为IAM登录帐号的密钥。</dd>
 *     <dt>{@literal armory.hw-obs.bucket}</dt>
 *     <dd>华为OBS的存储桶。OBS可以定义任意个存储桶，通常不同的项目使用不同的桶。</dd>
 * </dl>
 */
@Configuration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
public class HuaweiObsAutoConfiguration {
    @Bean("obsClient")
    @ConditionalOnProperty("armory.hw-obs.endpoint")
    public IObsClient obsClient(
            @Value("${armory.hw-iam.access-key}") String accessKey,
            @Value("${armory.hw-iam.security-key}") String secretKey,
            @Value("${armory.hw-obs.endpoint}") String endPoint
    ) {
        return new ObsClient(accessKey, secretKey, endPoint);
    }

    @Bean
    @ConditionalOnBean(IObsClient.class)
    @ConditionalOnWebApplication
    public HuaweiObsController huaweiObsController() {
        return new HuaweiObsController();
    }
}
